package com.yxt.common.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yxt.common.sys.CommConstant;
import com.yxt.model.Listener;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;

public class ListTableAdapter extends SimpleAdapter {
	private List<Map<String,String>> dataList;
	private List<Listener> itemListeners = new ArrayList<Listener>();
	private List<String> keyList = new ArrayList<String>();
	
	public ListTableAdapter(Context context, List<Map<String,String>> data,	int resource, String[] from, int[] to) {
		super(context, data, resource, from, to);
		this.dataList = data;
	}

	public ListTableAdapter(Context context, List<Map<String,String>> data,	int resource, String[] from, int[] to, List<Listener> items, List<String> keys) {
		super(context, data, resource, from, to);
		this.dataList = data;	
		keyList.addAll(keys);
		itemListeners.addAll(items);
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		 View view = super.getView(position, convertView, parent);		 
		int colorPos = position % CommConstant.Config.DEFAULT_TABLE_COLORS.length;
		view.setBackgroundColor(CommConstant.Config.DEFAULT_TABLE_COLORS[colorPos]);
		
		Map<String,String> data = dataList.get(position);
		for (int i = 0; i< itemListeners.size(); i++) {
			Listener item = itemListeners.get(i);
			View v = view.findViewById(item.getId());
			TextItemOnClickListener listener = item.getListener();
			Map<String,String> params = new HashMap<String,String>();
			for (int k = 0; k < keyList.size(); k++) {
				String key = keyList.get(k);
				params.put(key,data.get(key));
			}
			listener.setParameters(params);
			v.setOnClickListener(listener);
		}	
		
		return view;
	}
}
